﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using TTIServer;

namespace TTIServer.TTISettings.ExpressKeyLayouts
{
	/// <summary>
	/// Interaction logic for DTK2100EK.xaml
	/// </summary>
	public partial class DTK2100EK : UserControl
	{
		ContentControl myCC;
		private System.Windows.Controls.ContextMenu ExpressKeyMenu;

		private Menu ExpressKeyPopUpMenu;
		MenuItem NewItem;
		List<MenuItem> MenuSource;
		Storyboard sb;
		Storyboard pcSb;

		ContextMenu EKContextMenu;

		public bool ExpressKeysExtended;

		TabletSettings TabletSettingsRef;

		PenLayouts.PenLayout newPenLayout;

//	   calloutEKAction myCEKA;
       
        //class PenCanvasClass : ContentControl
        //{
            
        //    protected override 




        //}

        private static readonly RoutedEvent PenIsNull = EventManager.RegisterRoutedEvent("NullPenCanvas",
    RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Canvas));

        public event RoutedEventHandler PenContentNull
        {
            add { AddHandler(PenIsNull, value); }
            remove { RemoveHandler(PenIsNull, value); }
        }

        void RaisePenNullEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(PenIsNull);
            RaiseEvent(newEventArgs);
        }


		public void InitializeEKButtons( TabletSettings inTabletSettingsRef )
	   {

		   TabletSettingsRef = inTabletSettingsRef;

		   //ExpressKeyMenuSelection myEKMS 
			EKContextMenu = new ExpressKeyMenuSelection( TabletSettingsRef );


	   }

		public DTK2100EK()
		{
			InitializeComponent();


		}

		public void EKOutAnimation( )
		{

            sb = this.FindResource("EKmoving") as Storyboard;
			sb.Completed+=sb_Completed;
		  sb.Begin();
		  ExpressKeysExtended = false;

		}



        public void EKInAnimation()
        {
			this.EKSettingsCanvas.Visibility = Visibility.Visible;
             sb = this.FindResource("EKmoving_Reverse") as Storyboard;
            sb.Begin();
		  ExpressKeysExtended = true;
            
        }

		void sb_Completed(object sender, EventArgs e)
		{

			this.EKSettingsCanvas.Visibility = Visibility.Hidden;
			sb.Completed -= sb_Completed;


		}

		public void changePen( PenLayouts.PenLayout inPenLayout)
		{

			newPenLayout = inPenLayout;
			PenFadeOut();

		}

		public void PenFadeOut()
		{
			 pcSb = this.FindResource("PenCanvasFadeOut") as Storyboard;
			 pcSb.Completed += pcSb_Completed;
			pcSb.Begin();
		}

		void pcSb_Completed(object sender, EventArgs e)
		{
			if (PenCanvas.Children.Count > 0)
				PenCanvas.Children.Clear();

			if (newPenLayout != null)
			{
				PenCanvas.Children.Add(newPenLayout);
				PenFadeIn();
			}

		}

		public void PenFadeIn()
		{
			 pcSb = this.FindResource("PenCanvasFadeIn") as Storyboard;
			pcSb.Begin();
		}

		private void PenCanvas_SourceUpdated(object sender, DataTransferEventArgs e)
		{




		}

		private void label3_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			//TTIServer.TTISettings.EditPresets myEP = new TTIServer.TTISettings.EditPresets();
			//myEP.Owner = Application.Current.MainWindow;
			//myEP.WindowStartupLocation = WindowStartupLocation.Manual;
			
			////myEP.Left = e.GetPosition(this).X - (myEP.Width/2);
			////myEP.Top = e.GetPosition(this).Y - (myEP.Height / 2);
			
			//Storyboard sb = this.FindResource("BlurEKs") as Storyboard;
			//sb.Begin();
			//myEP.ShowDialog();
			//sb = this.FindResource("BlurEKs_Reverse") as Storyboard;
			//sb.Begin();
		}

		private void label_Click(object sender, RoutedEventArgs e)
		{

			if (myCEKA == null)
				myCEKA = new calloutEKAction();

			//Console.WriteLine(sender.ToString());
			Button theButton = (Button)sender;

			//myCEKA.Margin = new Thickness(0, 55, 0, 0);
			//myCEKA.HorizontalAlignment = HorizontalAlignment.Left;
			//myCEKA.VerticalAlignment = VerticalAlignment.Top;
			//PenCanvas.Children.Add(myCEKA);
			myCEKA.RenderTransform = new TranslateTransform(10,18.0);
			double pointForAnchor = (theButton.Margin.Top - myCEKA.Margin.Top) / 243;
			Console.WriteLine(pointForAnchor.ToString());
			


			
		}

		private void EKLabelL1_Click(object sender, RoutedEventArgs e)
		{
			Button ClickedButton = (Button)sender;
			
			//Need to show the context menu here
			EKContextMenu.PlacementTarget = ClickedButton;

			EKContextMenu.IsOpen = true;
		}
	}
}
